package com.cy.jt.service;

import com.cy.jt.vo.ImageVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/8/8 18:18
 */
@Service
@PropertySource("classpath:/properties/image.properties")
public class FileServiceImpl implements FileService{

    @Value("${image.localDirPath}")
    private String localDirPath;
    @Value("${image.localUrlPath}")
    private String localUrlPath;

    @Override
    public ImageVo upload(MultipartFile file) throws IOException {
        //1.1校验是否为图片类型  abc.jpg ABC.JPG 文件大小写
        String fileName = file.getOriginalFilename();
        //将所有的文件名称转化为小写
        fileName = fileName.toLowerCase();
        if(!fileName.matches("^.+\\.(jpg|png|gif)$")){
            return null;
        }
        //1.2 校验图片类型是否为木马
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if(width == 0 || height == 0 ){
                return null;
            }
        }catch (Exception e){
            return null;
        }
        //2. 目录结构
        //2.1 实现分目录存储...  可以以时间维度进行分隔 /yyyy/MM/dd/
        String datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
        //2.2 进行目录的拼接  "D:/JT_IMAGE/2021/11/11";
        String localDir = localDirPath + datePath;
        //2.3 需要创建目录
        File dirFile = new File(localDir);
        if (! dirFile.exists()){
            dirFile.mkdirs();
        }
        //3.文件名称重复  采用UUID防止文件重名 uuid.jpg
        String uuid
                = UUID.randomUUID().toString().replace("-", "");
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String realFileName = uuid + fileType;
        //c:jt_image/2021/11/11/uuid.jpg
        String fileEntirePath = localDir + realFileName;
        //实现文件上传
        File realFile = new File(fileEntirePath);
        file.transferTo(realFile);

        //封装ImageVO对象  //2021/11/11/uuid.jpg 图片路径
        String virtualPath = datePath + realFileName;
        //封装虚拟URl地址  http://image.jt.com/2021/11/11/uuid.jpg
        String urlPath = localUrlPath + virtualPath;
        return new ImageVo(virtualPath,urlPath,realFileName);

    }

    @Override
    public void deleteFile(String virtualPath) {
        // 根据虚拟路径 删除 本地磁盘文件
//        System.out.println(virtualPath);
        // 获取本地磁盘完整路径/2021/08/11/c06844c87d624d4d9d536b20793b2978.png
        String localDirEntirePath = localDirPath + virtualPath;
        File file = new File(localDirEntirePath);
        boolean delete = file.delete();
        System.out.println("文件删除 " + delete);
    }
}
